package com.jams.goodox.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jams.goodox.R;

/**
 * Created by Jin on 2016/7/7.
 * Description
 */
public class VonNeumannLayout extends RelativeLayout {

	TextView num;
	ImageView minus, add;

	int number = 0;
	int max = Integer.MAX_VALUE;
	boolean hasMaxToast = false;
	boolean hasMinToast = false;
	String maxToast, minToast;
	int min = 0;
	
	//是否可以点击
	private boolean isCouldClick = true;

	public VonNeumannLayout(final Context context, AttributeSet attrs) {
		super(context, attrs);
		View view = View.inflate(context, R.layout.von_neumann_layout, null);
		this.addView(view);

		num = (TextView) view.findViewById(R.id.num);
		num.setText(String.valueOf(number));

		minus = (ImageView) view.findViewById(R.id.num_minus);
		minus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isCouldClick) {
					if (number > min) {
						num.setText(String.valueOf(--number));
						if (null != mMinusEventListener)
							mMinusEventListener.controlMinusOneEventListener();
					} else {
						if (hasMinToast) Toast.makeText(context, minToast, Toast.LENGTH_LONG).show();
					}
					resetBtn();
				}
			}
		});

		add = (ImageView) view.findViewById(R.id.num_add);
		add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isCouldClick) {
					if (number < max) {
						num.setText(String.valueOf(++number));
						if (null != mAddEventListener)
							mAddEventListener.controlAddOneEventListener();
					} else {
						if (hasMaxToast) Toast.makeText(context, maxToast, Toast.LENGTH_LONG).show();
					}
					resetBtn();
				}
			}
		});
	}

	public void setCouldClick(boolean isCouldClick) {
		this.isCouldClick = isCouldClick;
	}
	
	public boolean isCouldClick() {
		return isCouldClick;
	}
	
	public int[] getAddIconCoor() {
		int[] c = new int[2];
		add.getLocationOnScreen(c);
		return c;
	}

	private int VonNeumannLayoutMode = 0;

	private void resetBtn() {
		if (VonNeumannLayoutMode == 0) {
			if (number == 0) {
				minus.setVisibility(GONE);
				num.setVisibility(GONE);
				add.setBackgroundResource(R.mipmap.von_add_single);
			} else {
				minus.setVisibility(VISIBLE);
				num.setVisibility(VISIBLE);
				minus.setBackgroundResource(R.mipmap.von_minus);
				add.setBackgroundResource(R.mipmap.von_add);
			}
		} else if (VonNeumannLayoutMode == 1) {
			if (number == 0)
				minus.setBackgroundResource(R.mipmap.von_minus_grey);
			else
				minus.setBackgroundResource(R.mipmap.von_minus);
		}
	}

	/**
	 * 初始化
	 * @param n int number
	 */
	public void initNum(int n) {
		if (n >= 0) {
			number = n;
			num.setText(String.valueOf(n));
			resetBtn();
		}
	}

	/**
	 * 充值数量
	 * @param n 数量
	 */
	public void resetNum(int n) {
		initNum(n);
	}

	public void setMax(int max) {
		this.max = max;
	}

	public void setMaxToast(String toast) {
		hasMaxToast = true;
		this.maxToast = toast;
	}

	public void setMinToast(String toast) {
		hasMinToast = true;
		this.minToast = toast;
	}

	public void setMin(int min) {
		this.min = min;
	}

	/**
	 * 获取结果
	 * @return number
	 */
	public int getResult() {
		return number;
	}

	private AddEventListener mAddEventListener;
	public interface AddEventListener {
		void controlAddOneEventListener();
	}
	private MinusEventListener mMinusEventListener;
	public interface MinusEventListener {
		void controlMinusOneEventListener();
	}
	public void AddControlEventListener(AddEventListener mAddEventListener, MinusEventListener mMinusEventListener) {
		this.mAddEventListener = mAddEventListener;
		this.mMinusEventListener = mMinusEventListener;
	}
}
